//
//  HXDTabBarController.m
//  qingDaoCircleOfLife
//
//  Created by huxuedong on 15/11/21.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

#import "HXDTabBarController.h"
#import "HXDFactory.h"

#define  kTabBarUpViewHeight 44

@interface HXDTabBarController ()

@property (weak, nonatomic) UIView *upView;
@property (weak, nonatomic) UIView *downView;

@end

@implementation HXDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.hidden = YES;
    //创建下部视图
    UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(0, kUIScreenHeight - kUITabBarHeight, kUIScreenWidth, kUITabBarHeight)];
    downView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:downView];
    self.downView = downView;
    //添加button
    for (int i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *normalImage = [NSString stringWithFormat:@"home_%d",i];
        [button setBackgroundImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
        NSString *selected = [NSString stringWithFormat:@"home_%d_pressed",i];
        [button setBackgroundImage:[UIImage imageNamed:selected] forState:UIControlStateSelected];
        button.frame = CGRectMake(i * (kUIScreenWidth / 5.0), 0, kUIScreenWidth / 5.0, kUITabBarHeight);
        button.tag = i;
        [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
        [self.downView addSubview:button];
        if (i == 0) {
            button.selected = YES;
        }
    }
    //添加上部视图
    UIView *upView = [[UIView alloc]initWithFrame:CGRectMake(0, kUIScreenHeight - kUITabBarHeight - kTabBarUpViewHeight, kUIScreenWidth, kTabBarUpViewHeight)];
    upView.backgroundColor = [UIColor blackColor];
    upView.alpha = 0.7;
    upView.hidden = YES;
    [self.view addSubview:upView];
    self.upView = upView;
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.upView.bounds];
    imageView.image = [UIImage imageNamed:@"home_topbar"];
    [self.upView addSubview:imageView];
    //添加按钮
    UIButton *buy = [UIButton buttonWithType:UIButtonTypeCustom];
    buy.frame = CGRectMake(0, 0, kUIScreenWidth * (160.0 / 640.0), kTabBarUpViewHeight);
    [buy addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
    [buy setTitle:@"联系商家" forState:UIControlStateNormal];
    buy.tag = 5;
    [self.upView addSubview:buy];
    UIButton *shake = [HXDFactory type:0 withFrame:CGRectMake(kUIScreenWidth * (150.0 / 640.0), 0, kUIScreenWidth * (200.0 / 640.0), kTabBarUpViewHeight) withTitle:@"摇一摇" withTarget:self withAction:@selector(changeViewController:)];
    shake.tag = 6;
    [self.upView addSubview:shake];
    UIButton *onlive = [HXDFactory type:0 withFrame:CGRectMake(kUIScreenWidth * (350.0 / 640.0), 0, kUIScreenWidth * (120.0 / 640.0), kTabBarUpViewHeight) withTitle:@"直播" withTarget:self withAction:@selector(changeViewController:)];
    onlive.tag = 7;
    [self.upView addSubview:onlive];
    UIButton *about = [HXDFactory type:0 withFrame:CGRectMake(kUIScreenWidth * ((640 - 170) / 640.0), 0, kUIScreenWidth * (150.0 / 640.0), kTabBarUpViewHeight) withTitle:@"关于" withTarget:self withAction:@selector(changeViewController:)];
    about.tag = 8;
    [self.upView addSubview:about];
}

//点击按钮执行的方法
- (void)changeViewController:(UIButton *)button {
    self.upView.hidden = YES;
    [self.downView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = (UIButton *)obj;
        button.selected = NO;
    }];
    button.selected = YES;
    if (button.tag < 4) {
        self.selectedIndex = button.tag;
    } else if (button.tag == 4) {
        self.upView.hidden = NO;
    } else {
        self.selectedIndex = button.tag - 1;
    }
}

- (void)isHiddenTabBar:(BOOL)flag {
    if (flag) {
        self.upView.hidden = YES;
        self.downView.hidden = YES;
    } else {
        self.downView.hidden = NO;
    }
}

//重写设置高亮的方法，取消点击时系统默认的设置（点击按钮时没变化）
- (void)setHighlighted:(BOOL)highlighted {
}

@end
